﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using System;
using System.Linq;

namespace ProjectTW.Foliage
{
    /// <summary>
    /// Some extensions that helps achieving things that aren't built in with unity.
    /// </summary>
    public static class ClassExtensions
    {
        /// <summary>
        /// Conver a terrain position to the world position
        /// </summary>
        /// <param name="pos">current position in local space</param>
        /// <param name="terrainScale">the size of the terrain this point is on</param>
        /// <param name="terrainPosition">the position of the terrain this point is on</param>
        /// <returns>the new position in world space</returns>
        public static Vector3 LocalToWorld(this Vector3 pos, Vector3 terrainScale, Vector3 terrainPosition)
        {
            return Vector3.Scale(pos, terrainScale) + terrainPosition; 
        }

        /// <summary>
        /// Convert a world space position to terrain position
        /// </summary>
        /// <param name="pos">the world space position</param>
        /// <param name="terrain">the terrain you want to get this position on</param>
        /// <returns>the new converted local spaced position.</returns>
        public static Vector3 WorldToLocal(this Vector3 pos, Terrain terrain)
        {
            return new Vector3(
                    pos.x / terrain.terrainData.size.x,
                    pos.y / terrain.terrainData.size.y,
                    pos.z / terrain.terrainData.size.z) - terrain.transform.position;
        }
    }
}
